package com.monkey.articlems.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.monkey.articlems.vo.ArticleVo;
import com.monkey.articlems.vo.Resp;
import com.monkey.articlems.service.ArticleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author monkey
 * @since 2020-05-30
 */
@Slf4j
@Controller
@RequestMapping("/articlems/article")
@Api(value = "文章接口")
public class ArticleController {

    @Autowired
    private ArticleService articleService;

    @ApiOperation(value = "查询所有")
    @ResponseBody
    @GetMapping("list")
    public Resp<ArticleVo> queryAll(
            @RequestParam("page") Long page,
            @RequestParam("limit") Long limit,
            @RequestParam(value = "keyword", required = false) String keyword
    ) {
//        如果keyword不为空，则拼接查询条件
        QueryWrapper<ArticleVo> queryWrapper = null;
        if (StringUtils.isNotBlank(keyword)) {
            queryWrapper = new QueryWrapper<>();
            queryWrapper.like("title", keyword).or().like("categories", keyword);
        }
        Page<ArticleVo> articleVoPage = new Page<>(page, limit);
        Page<ArticleVo> pageResult = this.articleService.page(articleVoPage, queryWrapper);
        if (pageResult == null) {
            return null;
        }
        long total = pageResult.getTotal();
        List<ArticleVo> records = pageResult.getRecords();
        return new Resp<ArticleVo>("0", "ok", total, records);
    }

    @ApiOperation(value = "删除用户")
    @ResponseBody
    @PostMapping("delete")
    public Resp<ArticleVo> deleteOne(@RequestBody HashMap<String, ArrayList<ArticleVo>> hashMap) {
        ArrayList<ArticleVo> articleVos = (ArrayList<ArticleVo>) hashMap.get("data");
        List<Integer> idList = articleVos.stream().map(ArticleVo::getId).collect(Collectors.toList());
        boolean flag = this.articleService.removeByIds(idList);
        if (!flag) {
            return new Resp<ArticleVo>("400", "删除失败", 0L, null);
        }
        return new Resp<ArticleVo>("300", "删除成功", 1L, null);
    }

    @ApiOperation(value = "用户更新")
    @ResponseBody
    @PostMapping("update")
    public Resp<ArticleVo> updateOne(@RequestBody ArticleVo articleVo) {
        boolean flag = this.articleService.updateById(articleVo);
        if (!flag) {
            return new Resp<ArticleVo>("400", "更新失败", 0L, null);
        }
        return new Resp<ArticleVo>("300", "更新成功", 1L, null);
    }
}

